package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neymar.entity.User;
import com.neymar.mapper.UserMapper;
import com.neymar.service.UserService;
import com.neymar.util.PageUtils;
import com.neymar.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteUserById(String userId) {
        return userMapper.deleteById(userId);
    }

    @Override
    public int updateUserById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User findUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getUserId()).eq("user_password", user.getUserPassword());
        User findUser = userMapper.selectOne(wrapper);
        if (findUser != null) {
            findUser.setUserPassword("");
            return findUser;
        }
        return null;
    }

    @Override
    public int userNumber() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as number");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        return ((Number) maps.get(0).get("number")).intValue();
    }

    @Override
    public UserVO findAllUser(Integer current, Integer size) {
        // 分页查询
        IPage<User> iPage = new Page<>();
        iPage.setCurrent(current);
        iPage.setSize(size);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("user_id", "user_name", "user_role");
        iPage = userMapper.selectPage(iPage, wrapper);

        UserVO userVO = new UserVO();
        userVO.setCurrent(current);
        userVO.setSize(size);
        userVO.setPages(iPage.getPages());
        userVO.setTotal(iPage.getTotal());
        userVO.setUsers(iPage.getRecords());
        return userVO;
    }

    @Override
    public UserVO findAllUserDetails(Integer current, Integer size) {

        // 获取记录
        List<Map<String, String>> allUserDetails = userMapper.findAllUsersDetails(current, size);
        UserVO userVO = new UserVO();
        userVO.setCurrent(current);
        userVO.setSize(size);
        userVO.setUsersDetails(allUserDetails);

        Long total = (long) allUserDetails.size();

        // 获取总的页数
        Long page = PageUtils.getPage(total, size);
        userVO.setPages(page);
        userVO.setTotal(total);

        return userVO;
    }

    @Override
    public User findOneById(String userId) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        User user = userMapper.selectOne(wrapper);
        return user;
    }


    @Override
    public String findById(String userId) {
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        User user = userMapper.selectOne(wrapper);
        return user.getUserName();
    }
}
